/**
 * Created by guodong on 2017/7/17.
 */
'use strict';
angular.module('app')
    .controller('informationController', [ '$rootScope', '$scope', '$http', '$state','$timeout',
        function($rootScope, $scope, $http, $state, $timeout) {
            $scope.title = '资讯分类管理';
            $scope.searchParams = { };
            $scope.loading = false;
            $scope.param={};
            $scope.search = function () {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name+'/information/read/list',
                    data: $scope.searchParams
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;

                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            // $scope.search();

            $scope.searchRe = function () {
                $scope.loading = true;
                $scope.searchParams.pageNum = 1;
                $.ajax({
                    url : $scope.app.project_name+'/information/read/list',
                    data: $scope.searchParams
                }).then(function(result) {

                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.search();

            $scope.clearSearch = function() {
                $scope.searchParams= { };
                $scope.search();
            }

            // 翻页
            $scope.pagination = function (pageNo) {
                $scope.searchParams.pageNum=pageNo;
                $scope.search();
            };

            // 删除
            $scope.deleteByModule=function (moduleNo) {
                $scope.alertModel({
                    text:'确定删除？',
                    btnCancel:true,
                    resCode:"10000"
                }).then(function(){
                    $.ajax({
                        url : $scope.app.project_name+'/information/delete',
                        data: {moduleNo:moduleNo}
                    }).then(function(result) {
                        $scope.loading = false;
                        $scope.alertModal({"content":"操作成功"});
                        $timeout(function(){
                            $scope.search();
                        },1000);
                        if (result.httpCode != 200) {
                            $scope.alertModal(result.msg);

                        }
                    });

                })

            }

            //资讯平台下拉列表信息
            $scope.changeParents = function () {
                $.ajax({
                    url : $scope.app.project_name+ '/information/getPlatselect',
                    data: { }
                }).then(function(result) {
                    if (result.httpCode == 200) {
                        $scope.platInfoListName = result.data;
                        console.log(result.data);
                    } else {
                        $scope.msg = result.msg;
                    }
                });
                $scope.searchRe();
            }
            $scope.changeParents();

            // 鼠标移入的动作
            $scope.introduction = [];
            $scope.preview = function(introduction){
                var showDiv = document.getElementById('showDiv');
                showDiv.style.left = event.clientX;
                showDiv.style.top = event.clientY;
                showDiv.style.display = 'block';
                showDiv.innerHTML = introduction;
            }
              // 鼠标移出的动作
            $scope.outHide = function(){
                var showDiv = document.getElementById('showDiv');
                showDiv.style.display = 'none';
                showDiv.innerHTML = '';
            }


            // 拖拽排序
            $scope.editSort=function ($event) {
                if($('#moduleNo').val()){
                    $scope.alertModal({'content':'模块名称为空方可拖拽排序'});
                    return;
                }
                if($('#banEnable').val()){
                    $scope.alertModal({'content':'启用禁用状态为空方可拖拽排序'});
                    return;
                }

                $("#informationList").sortable({opacity: 0.5,//拖动的透明度
                    revert: true, //缓冲效果
                    cursor: 'move', //拖动的时候鼠标样式
                    connectWith: ".column", scrollSpeed: 40});
                $("#informationList").disableSelection();

                for (var i = 0; i < $scope.pageInfo.records.length; i++) {
                    $(".editSortBtn").hide();
                    $(".saveSortBtn").show();
                }
            }

            $scope.saveSort=function ($event) {
                var batchUpdates = new Array();//声明一个数组来保存多个的id值
                var sortArray = new Array();//声明一个数组来保存多个序号的值
                for (var j = 0; j < $scope.pageInfo.records.length; j++) {
                    var tr = $(".sort  tbody tr")[j];
                    var id = $($(tr).find("td")[1]).html();
                    var sort = j + 1;
                    sortArray[j] = sort;//用来存放序号
                    var batchUpdate = id + "-" + sort;
                    batchUpdates[j] = batchUpdate;
                }
                $.ajax({
                    url: $scope.app.project_name + '/information/editSort',
                    data: {'ids': batchUpdates},
                    traditional: true
                }).then(function (result) {
                    if (result.httpCode == 200) {
                        $scope.alertModal({"content": "操作成功"});
                        $(".editSortBtn").show();
                        $(".saveSortBtn").hide();
                        $("#informationList").sortable('destroy');//解除拖動
                        $timeout(function () {
                            $scope.search();
                        }, 1000);
                    } else {
                        $scope.msg = result.msg;
                    }
                });
            }
            // 启用和禁用
            $scope.oper = function (moduleNo,$event) {

                $.ajax({
                    url : $scope.app.project_name+'/information/delete',
                    data: {moduleNo:moduleNo,enable:$event.target.checked?'Y':'N'}
                }).then(function(result) {
                    $scope.loading = false;
                    $scope.alertModal({"content":"操作成功"});
                    if (result.httpCode != 200) {
                        $scope.alertModal(result.msg);
                    }
                    $scope.search();
                });
            }

        } ]);